package cn.kent;


import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Java内置四大核心类型的函数式接口：
 * 1. 消费性 {@link java.util.function.Consumer<T>#accept(T t)}
 * 2. 供给型 {@link java.util.function.Supplier<T>#get() T}
 * 3. 函数型 {@link java.util.function.Function<T,R>#apply(T t)}
 * 4. 断定型 {@link java.util.function.Predicate<T>#test(T t)}
 */
public class FunctionInterfaceTest0 {

    @Test
    public void test1(){
        happyTime(500, new Consumer<Double>() {
            @Override
            public void accept(Double money) {
                System.out.println("学习太累了，去天上人间买瓶矿泉水，价格 = " + money);
            }
        });

        System.out.println("Lambda 表达式写法");

        happyTime(400, money-> System.out.println("学习太累了，去天上人间买瓶矿泉水，价格 = " + money));
    }

    public void happyTime(double money, Consumer<Double> con){
        con.accept(money);
    }

    @Test
    public void test2(){
        List<String> list = Arrays.asList("北京","南京","天津","东京","普京");
        final List<String> filterStringList = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterStringList);

        final List<String> filterString = filterString(list, s -> s.contains("京"));
        System.out.println(filterString);
    }

    public List<String> filterString(List<String> list, Predicate<String> pre){
        final ArrayList<String> filterList = new ArrayList<>();
        for (String s : list) {
            if (pre.test(s))
                filterList.add(s);
        }
        return filterList;
    }

}
